package com.study.leetcode;

public class Solution_240 {
    public boolean searchMatrix1(int[][] matrix, int target) {
        for (int[] ints : matrix) {
            if (ints[0] <= target && target <= ints[ints.length - 1]) {
                for (int anInt : ints) {
                    if (anInt == target) return true;
                }
            }
        }
        return false;
    }
    public boolean searchMatrix(int[][] matrix, int target) {
        if (matrix == null || matrix.length == 0) return false;
        int m = 0;
        int n = matrix[0].length - 1;
        while (m < matrix.length && n >= 0) {
            if (matrix[m][n] == target) {
                return true;
            } else if (matrix[m][n] > target) {
                n--;
            } else {
                m++;
            }
        }
        return false;
    }
}
